package com.example.shop.goods.dao;

import com.example.shop.goods.dto.EvaluationDetailBrowseInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EvaluationDetailBrowseInfoDao {
    public static final String ALL_FIELDS_SQL = EvaluationBrowseInfoDao.ALL_FIELDS_SQL + "";

    @Results(id = "EvaluationDetailBrowseInfoResultMap",value = {
            @Result(column = "evaluation_id",property = "evaluationId"),
            @Result(column = "user_id",property = "evaluationUser",
                    one = @One(select = "com.example.shop.user.dao.UserBriefInfoDao.selectById")),
            @Result(column = "evaluation_type",property = "evaluationType"),
            @Result(column = "evaluation_id",property = "thumbUpUserList",
                    many = @Many(select = "com.example.shop.user.dao.UserAvatarDao.selectByEvaluationId")),
            @Result(column = "evaluation_id",property = "evaluationPictureList",
                    many = @Many(select = "com.example.shop.mapper.EvaluationPictureMapper.selectPictureById")),
            @Result(column = "evaluation_id",property = "thumbUpNumber",
                    one = @One(select = "com.example.shop.mapper.EvaluationUserThumbUpMapper.count")),
            @Result(column = "order_id",property = "userPurchasedGoods",
                    one = @One(select = "com.example.shop.goods.dao.UserPurchasedGoodsDao.selectByOrderId")),
            @Result(column = "evaluation_id",property = "commentsList",
                    many = @Many(select = "com.example.shop.goods.dao.CommentsDao.selectByEvaluationId")),
            @Result(column = "evaluation_id",property = "commentsReplyList",
                    many = @Many(select = "com.example.shop.goods.dao.CommentsReplyDao.selectByEvaluationId")),
    })
    @Select("select " + ALL_FIELDS_SQL +
            "from evaluation e " +
            "where e.evaluation_id = ${evaluationId} ")
    EvaluationDetailBrowseInfo selectById(@Param("evaluationId") Long evaluationId);

    @ResultMap("EvaluationDetailBrowseInfoResultMap")
    @Select("<script>" +
            "select " + ALL_FIELDS_SQL +
            "from evaluation e " +
            "where e.evaluation_id in " +
            "<foreach collection='evaluationIdList' item='evaluationId' index='index'  open = '(' separator=',' close = ')' >" +
                "${evaluationId}" +
            "</foreach>" +
            " " +
            "</script>")
    List<EvaluationDetailBrowseInfo> batchSelectById(@Param("evaluationIdList") List<Long> evaluationIdList);

    @Delete("DELETE evaluation,r_evaluation_comments,r_evaluation_comments_reply " +
            "from " +
            "evaluation " +
            "left join r_evaluation_comments on evaluation.evaluation_id = r_evaluation_comments.evaluation_id " +
            "left join r_evaluation_comments_reply on evaluation.evaluation_id = r_evaluation_comments_reply.evaluation_id " +
            "where evaluation.evaluation_id = ${evaluationId}")
    int deleteEvaluation(long evaluationId);
}
